package com.hh.storeRoom.service.impl;

import com.hh.commen.mapper.CommonMapper;
import com.hh.storeLogo.mapper.StoreLogoMapper;
import com.hh.storeRoom.bean.StoreAreaStatus;
import com.hh.storeRoom.bean.StoreRoom;
import com.hh.storeRoom.mapper.StoreRoomMapper;
import com.hh.storeRoom.service.StoreRoomService;
import com.hh.storeRoom.vo.StoreRoomAddVo;
import com.hh.storeRoom.vo.StoreRoomUpdateStatusVo;
import com.hh.storeRoom.vo.StoreRoomUpdateVo;
import com.hh.storeRoom.vo.StoreRoomVo;

import com.hh.utils.PageVo;
import com.hh.utils.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class StoreRoomServiceImpl implements StoreRoomService {

    @Autowired
    private StoreRoomMapper storeRoomMapper;

    @Autowired
    private StoreLogoMapper storeLogoMapper;

    @Autowired
    private CommonMapper commonMapper;

    //查询分页
    @Override
    public ResultVo queryPage(StoreRoomVo storeRoomVo) {
        //计算分页第一个参数
        Integer start = (storeRoomVo.getPageIndex()-1)* storeRoomVo.getPageIndex();
        storeRoomVo.setStart(start);
        //查询页面显示的数据
        List<StoreRoom> storeRoomList = storeRoomMapper.getStoreRoomByPage(storeRoomVo);
        //计算总条数
        Integer totalSize = storeRoomMapper.getTotalSize();
        //计算总页数
        Integer totalPage = (totalSize/ storeRoomVo.getPageSize() == 0 ? totalSize % storeRoomVo.getPageSize():totalSize % storeRoomVo.getPageSize()+1);
        //将数据放到对象中
        PageVo<StoreRoom> pageVo = new PageVo<>();
        pageVo.setTotalPage(totalPage);
        pageVo.setTotalNumber(totalSize);
        pageVo.setDataList(storeRoomList);
        return ResultVo.success(pageVo);
    }

    //新增
    @Override
    @Transactional
    public ResultVo add(StoreRoomAddVo storeRoomAddVo) {
        //判断库房的编码是否唯一
        Integer codeCount = storeRoomMapper.getCountByCode(storeRoomAddVo.getCode());
        if (codeCount>0){
            //如果编码不唯一,提示错误信息
            return ResultVo.error("库房编码重复,请从新输入");
        }
        //判断库房的名称是否唯一
        Integer nameCount = storeRoomMapper.getCountByName(storeRoomAddVo.getName());
        if (nameCount>0){
            //如果名称不唯一,提示错误信息
            return ResultVo.error("库房名称重复,请从新输入");
        }
        //判断库房标识是否为启用
        Boolean status = storeLogoMapper.checkStatus(storeRoomAddVo.getLogoCode());
        if(status!=null){
            if (!status){
                return ResultVo.error("当前库房标识为禁用状态,请从新添加");
            }
        }
        //判断地区所选字段是否有效
        Integer region = commonMapper.checkRegion(storeRoomAddVo.getRegionName());
        if(region==0){
            return ResultVo.error("当前所选地区有误,请重新选择");
        }
        //判断国家所选字段是否有效
        Integer country = commonMapper.checkCountry(storeRoomAddVo.getCountryCode());
        if(country==0){
            return ResultVo.error("当前所选国家有误,请重新选择");
        }
        //判断所选省是否有效
        Integer province = commonMapper.checkProvince(storeRoomAddVo.getProvinceCode());
        if(province==0){
            return ResultVo.error("当前所选省份有误,请重新选择");
        }
        //判断市所选字段是否有效
        Integer city = commonMapper.checkCity(storeRoomAddVo.getCityCode());
        if(city==0){
            return ResultVo.error("当前所选城市有误,请重新选择");
        }
        //判断区县所选字段是否有效
        Integer county = commonMapper.checkCounty(storeRoomAddVo.getCountyCode());
        if(county==0){
            return ResultVo.error("当前所选区县有误,请重新选择");
        }
        try {

            storeRoomMapper.add(storeRoomAddVo);
        }catch (Exception e){
            log.error(e.getMessage());
            return ResultVo.error("添加失败,请重新添加");
        }

        return ResultVo.success("添加成功");
    }

    //修改
    @Override
    @Transactional
    public ResultVo update(StoreRoomUpdateVo storeRoomUpdateVo) {
        //判断当前操作信息是否为启用状态
        Boolean status = storeRoomMapper.checkStatus(storeRoomUpdateVo.getCode());
        if (status){
            return ResultVo.error("当前库房为启用状态,禁止修改");
        }
        //判断库房的名称是否唯一
        Integer nameCount = storeRoomMapper.getCountByName2(storeRoomUpdateVo);
        if (nameCount>0){
            //如果名称不唯一,提示错误信息
            return ResultVo.error("库房名称重复,请从新输入");
        }
        //判断库房标识编码是否为启用状态
        Boolean logoStatus = storeLogoMapper.checkStatus(storeRoomUpdateVo.getLogoCode());
        if (!logoStatus){
            return ResultVo.error("当前库房标识为禁用状态,请从新添加");
        }
        //修改库房状态,判断库区库位是否都为禁用
        List<StoreAreaStatus> statusList = storeRoomMapper.getstatusList(storeRoomUpdateVo.getCode());
        if (statusList!=null){
            for (StoreAreaStatus storeAreaStatus : statusList) {
                //判断是否为空
                if(storeAreaStatus!=null){
                    //若有启用状态的,则不允许修改
                    if(storeAreaStatus.getStoreStatus()||storeAreaStatus.getAreaStatus()){
                        return ResultVo.error("当前库房不能禁用");
                    }
                }
            }
        }
        try {
            storeRoomMapper.update(storeRoomUpdateVo);
        }catch(Exception e){
            log.error(e.getMessage());
            return ResultVo.error("修改失败");
        }
        return ResultVo.success("修改成功");
    }

    //修改状态
    @Override
    @Transactional
    public ResultVo udapteStatus(StoreRoomUpdateStatusVo storeRoomUpdateStatusVo) {
        //修改库房状态,判断库区库位是否都为禁用
        List<StoreAreaStatus> statusList = storeRoomMapper.getstatusList2(storeRoomUpdateStatusVo.getId());
        if (statusList!=null){
            for (StoreAreaStatus storeAreaStatus : statusList) {
                if(storeAreaStatus!=null){
                    //若有启用状态的,则不允许修改
                    if(storeAreaStatus.getStoreStatus()||storeAreaStatus.getAreaStatus()){
                        return ResultVo.error("当前库房不能禁用");
                    }
                }
            }
        }
        try {
            storeRoomMapper.updateStatus(storeRoomUpdateStatusVo);
        }catch(Exception e){
            log.error(e.getMessage());
            return ResultVo.error("状态修改失败");
        }
        return ResultVo.success("状态修改成功");
    }
}
